package com.compass.mobile.ui.activity

import LogUtil
import UserInfoManager
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.leanback.widget.ArrayObjectAdapter
import androidx.leanback.widget.FocusHighlight
import androidx.leanback.widget.FocusHighlightHelper
import androidx.lifecycle.lifecycleScope
import com.compass.mobile.callback.OnItemClickListener
import com.compass.mobile.databinding.ActivityReportTypeBinding
import com.compass.mobile.framework.base.BaseVBActivity
import com.compass.mobile.framework.utils.dpToPx
import com.compass.mobile.models.Dictionaries
import com.compass.mobile.presenters.DictionariesPresenter
import com.compass.mobile.presenters.ReportPresenter
import com.compass.mobile.widget.MyBridgeAdapter
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import toBeanOrNull
import toJson

/**
 * Description： 报告类型
 * Date:2024/6/29 9:24
 */
class MyReportTypeActivity : BaseVBActivity<ActivityReportTypeBinding>() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, MyReportTypeActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var list: MutableList<Dictionaries>? = null

    override fun initView(savedInstanceState: Bundle?) {
        list = UserInfoManager.getInspectionReportType()?.toBeanOrNull<MutableList<Dictionaries>>()
        LogUtil.d("list==${list?.toJson()}")
        initReportType(list)
    }


    @SuppressLint("RestrictedApi")
    fun initReportType(dictionaries: MutableList<Dictionaries>?) {
        mBinding.gridCase.setNumColumns(4)
        mBinding.gridCase.verticalSpacing = dpToPx(10)
        mBinding.gridCase.horizontalSpacing = dpToPx(10)
        val arrayObjectAdapter = ArrayObjectAdapter(ReportPresenter())
        val itemBridgeAdapter = MyBridgeAdapter(
            this,
            arrayObjectAdapter, onItemClickListener
        )
        mBinding.gridCase.setAdapter(itemBridgeAdapter)
        arrayObjectAdapter.addAll(0, dictionaries)
        FocusHighlightHelper.setupBrowseItemFocusHighlight(
            itemBridgeAdapter,
            FocusHighlight.ZOOM_FACTOR_SMALL, false
        )
        lifecycleScope.launch {
            delay(500)
            //延迟获取焦点
            mBinding?.gridCase?.requestFocus()
        }


    }

    private val onItemClickListener: OnItemClickListener = object : OnItemClickListener {
        override fun onItemClick(position: Int, view: View) {
           val value= list?.get(position)?.value
            MyReportActivity.start(this@MyReportTypeActivity,value)
        }
    }
}